﻿using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.EntityFrameworkCore;
using PathologyDAL.DB;
using PathologyDAL.Modle;

namespace PathologyDAL.Service
{
    public class TumourServices
    {
        public async Task<int> AddTumorsAsync(List<string> names)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                List<Tumour> bvs = new List<Tumour>();
                //查询数据库中是否有
                foreach (string name in names)
                {
                    var bv = context.Tumours.FirstOrDefault(b => b.Name == name);

                    if (bv == null)
                    {
                        Tumour tumour = new Tumour
                        {
                            Name = name.Trim(),
                            LastUpdated = DateTime.Now,
                        };
                        await context.Tumours.AddAsync(tumour);
                    }
                }

                return await context.SaveChangesAsync();
            }
        }

        /// <summary>
        /// 将移到右侧的添加为肿瘤的特性
        /// </summary>
        /// <param name="rigthTumours"></param>
        /// <param name="currentFilter"></param>
        /// <param name="selectedIndex"></param>
        /// <returns></returns>
        public async Task AddFilterByFiterAndPossibleAsync(
            List<int> rigthTumours,
            FilterCondition currentFilter,
            int selectedIndex
        )
        {
            //概率
            Possibility possibility = (Possibility)selectedIndex;

            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                //找到要添加的


                foreach (int id in rigthTumours)
                {
                    //从中找出来
                    var currentTumour = await context.TumourConditions.SingleOrDefaultAsync(t =>
                        t.Tumour == id && t.FilterCondition == currentFilter.Id
                    );
                    if (null == currentTumour)
                    {
                        TumourCondition tumourCondition = new TumourCondition();
                        tumourCondition.Tumour = id;
                        tumourCondition.FilterCondition = currentFilter.Id;
                        tumourCondition.Possibility = possibility;

                        context.TumourConditions.Add(tumourCondition);
                    }
                    else
                    {
                        currentTumour.Possibility = possibility;
                    }
                }

                await context.SaveChangesAsync();
            }
        }

        public async Task<List<Tumour>> FindTumourWithFilterStringAsync(string searchParm)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                if (string.IsNullOrEmpty(searchParm))
                {
                    return await context
                        .Tumours.OrderByDescending(t => t.LastUpdated)
                        .ToListAsync();
                }
                else
                {
                    return await context
                        .Tumours.Where(t => t.Name.Contains(searchParm))
                        .OrderByDescending(t => t.LastUpdated)
                        .ToListAsync();
                }
            }
        }

        //按ID找
        public async Task<FilterCondition> GetFilterByIDAsync(int iD)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                return await context.Conditions.SingleOrDefaultAsync(f => f.Id == iD);
            }
        }

        //新增

        public async Task<int> AddTumourAsync(Tumour tumour)
        {
            if (tumour == null)
            {
                throw new ArgumentNullException("输入的参数为null");
            }

            tumour.LastUpdated = DateTime.Now;
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                await context.Tumours.AddAsync(tumour);

                return await context.SaveChangesAsync();
            }
        }

        public async Task<int> DeleteAsync(int id)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                var tExist = await context.Tumours.SingleOrDefaultAsync(t => t.ID == id);

                if (tExist != null)
                {
                    var tumourCondiExist = context.TumourConditions.Where(t =>
                        t.Tumour == tExist.ID
                    );
                    context.TumourConditions.RemoveRange(tumourCondiExist);

                    context.Tumours.Remove(tExist);
                }

                return await context.SaveChangesAsync();
            }
        }

        public async Task<bool> CheckNameExistAsync(string Name)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                return await context.Tumours.AnyAsync(t => t.Name == Name);
            }
        }

        public async Task<List<Tumour>> CheckNameExistAsync(List<string> names)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                return await context.Tumours.Where(t => names.Contains(t.Name)).ToListAsync();
            }
        }

        public async Task<List<TumourCondition>> QueryByFilterAnsy(List<int> checkedConditions)
        {
            //查询的时候必须保证肿瘤里包含这个的所有结果

            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                //查到所有的信息
                return await context
                    .TumourConditions.Where(t => checkedConditions.Contains(t.FilterCondition))
                    .ToListAsync();
            }
        }

        public async Task RemoveFilterByFiterAndPossibleAsync(
            List<int> leftTumour,
            FilterCondition currentFilter,
            int selectedIndex
        )
        {
            Possibility possibility = (Possibility)selectedIndex;
            //左侧的全部要移除，修改满足条件的


            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                foreach (int id in leftTumour)
                {
                    var currentTumour = context
                        .TumourConditions.Where(t => t.Tumour == id)
                        .Where(t => t.Possibility == possibility)
                        .Where(t => t.FilterCondition == currentFilter.Id);
                    context.TumourConditions.RemoveRange(currentTumour);
                }

                await context.SaveChangesAsync();
            }
        }

        public async Task<int> UpdateNameAsync(Tumour selectTumour)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                //需要检查已经被删除的

                var tumours = context.Tumours.Single(t => t.ID == selectTumour.ID);
                tumours.Name = selectTumour.Name;

                var tumoursDels = await context.Tumours.FirstOrDefaultAsync(t =>
                    t.Name == selectTumour.Name
                );
                if (null != tumoursDels)
                {
                    tumoursDels.Name = $"被ID{selectTumour.ID}占用了名称";
                }

                return await context.SaveChangesAsync();
            }
        }

        public async Task<List<Tumour>> LoadByConditionAndPossibilityAsync(
            FilterCondition currentFilter,
            Possibility possibility
        )
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                List<Tumour> tumours = new List<Tumour>();

                var tumourWith = await context
                    .TumourConditions.Where(t => t.FilterCondition == currentFilter.Id)
                    .Where(t => t.Possibility == possibility)
                    .ToListAsync();
                foreach (TumourCondition tumourCondition in tumourWith)
                {
                    var tumour = await context.Tumours.SingleAsync(t =>
                        t.ID == tumourCondition.Tumour
                    );
                    tumours.Add(tumour);
                }

                return tumours;
            }
        }

        /// <summary>
        /// 不包含着个概率的
        /// </summary>
        /// <param name="currentFilter"></param>
        /// <param name="possibility"></param>
        /// <returns></returns>
        public async Task<List<Tumour>> LoadNotByConditionAndPossibilityAsync(List<Tumour> right)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                //

                return await context.Tumours.Where(c => !right.Contains(c)).ToListAsync();
            }
        }

        public async Task<List<TumourConditionDisPlay>> LoadTumourConditionByTumourAsync(
            Tumour tumour
        )
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                List<TumourConditionDisPlay> conditionDisPlays = new List<TumourConditionDisPlay>();

                var f = context.TumourConditions.Where(t => t.Tumour == tumour.ID);
                foreach (TumourCondition condition in f)
                {
                    TumourConditionDisPlay tumourConditionDisPlay = new TumourConditionDisPlay(
                        condition
                    );
                    tumourConditionDisPlay.TumourInfo = tumour;
                    tumourConditionDisPlay.FilterConditionInfo =
                        await context.Conditions.SingleOrDefaultAsync(t =>
                            t.Id == condition.FilterCondition
                        );
                    conditionDisPlays.Add(tumourConditionDisPlay);
                }
                return conditionDisPlays;
            }
        }

        public async Task<bool> CheckTumourConditionsExistAsync(
            Tumour tumour,
            FilterCondition selectCondition
        )
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                return await context.TumourConditions.AnyAsync(t =>
                    t.Tumour == tumour.ID && t.FilterCondition == selectCondition.Id
                );
            }
        }

        public async Task<int> AddTumourConditionAsync(
            Tumour tumour,
            FilterCondition selectCondition,
            int possibleInt
        )
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                context.TumourConditions.Add(
                    new TumourCondition
                    {
                        Possibility = (Possibility)possibleInt,
                        Tumour = tumour.ID,
                        FilterCondition = selectCondition.Id,
                    }
                );
                return await context.SaveChangesAsync();
            }
        }

        public async Task<int> UpdatePossibleAsync(TumourConditionDisPlay eidtorFilter)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                var possibleintm = context.TumourConditions.SingleOrDefault(t =>
                    t.id == eidtorFilter.TumourCondition.id
                );
                if (null != possibleintm)
                {
                    possibleintm.Possibility = eidtorFilter.TumourCondition.Possibility;
                }
                return await context.SaveChangesAsync();
            }
        }

        public async Task<int> UpdatePossibleItemAsync(TumourConditionDisPlay eidtorFilter)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                var possibleintm = context.TumourConditions.SingleOrDefault(d =>
                    d.id == eidtorFilter.TumourCondition.id
                );
                if (null != possibleintm)
                {
                    possibleintm.Possibility = eidtorFilter.TumourCondition.Possibility;
                    possibleintm.FilterCondition = eidtorFilter.TumourCondition.FilterCondition;
                }
                return await context.SaveChangesAsync();
            }
        }

        public async Task<int> DeletePossibleItemAsync(TumourConditionDisPlay eidtorFilter)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                var possibleintm = context.TumourConditions.SingleOrDefault(c =>
                    c.id == eidtorFilter.TumourCondition.id
                );
                if (null != possibleintm)
                {
                    context.TumourConditions.Remove(possibleintm);
                }
                return await context.SaveChangesAsync();
            }
        }
    }
}
